<?php

class Product extends Controller
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::Controller();

		// Check if the logged user is an administrator
		$this->access_library->check_access();

		// Load needed models, libraries, helpers and language files
		$this->load->module_model('admin', 'product_model', 'product');
		$this->load->module_model('admin', 's_category_model', 's_category');

		$this->load->module_language('admin', 'general');
		$this->load->module_language('admin', 'product');
		//$this->load->library('fckeditor',array('instanceName' => 'content'));
	}

	// Public methods
	public function index()
	{

		$lang_working = load_language_working();
		$data['posts'] = $this->product->get_posts($lang_working['id']);
		$this->_template['page']	= 'product/list';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}

	public function create()
	{
		//$this->load->library('fckeditor',array('instanceName' => 'content','BasePath' => base_url().'system/plugins/fckeditor/','ToolbarSet' => 'Basic'));
		$lang_working = load_language_working();
		$this->load->helper('fckeditor');

		$this->form_validation->set_rules('title', 'lang:form_title', 'required|max_length[200]|xss_clean');
		//$this->form_validation->set_rules('excerpt', 'lang:form_excerpt', 'required|prep_for_form');
		//$this->form_validation->set_rules('content', 'lang:form_content', 'prep_for_form');
		$this->form_validation->set_rules('categories', 'lang:form_categories', 'required');
		$this->form_validation->set_rules('sticky', 'lang:form_sticky', 'numeric');
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$data['settings']['allow_comments'] = 'checked';

		$config['upload_path'] = './uploads/products';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '100';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		
		$this->load->library('upload', $config);
		
		$this->upload->initialize($config);
		$data['categories'] 		= $this->product->get_categories($lang_working['id']);
		
		if ($this->form_validation->run() == TRUE)
		{
			
			if ( ! $this->upload->do_upload())
			{
				$this->session->set_flashdata('message', $this->upload->display_errors());
				$this->_template['page']	= 'product/create';
			
				$this->system_library->load($this->_template['page'], null, TRUE);
			}	
			else
			{
					$data =  $this->upload->data();	
					//var_dump($data);exit();
					$this->load->library('image_lib');
					// Large
					$config = array(
							'image_library'=>'gd2',
							'source_image'=>$data['full_path'],
							'thumb_maker' => '_170x198',
							'create_thumb'=>FALSE,
							'maintain_ratio'=>FALSE,
							'quality'=>'100%',
							'width'=>'300',
							'height'=>'350',
							'new_image'=>$data['file_path'].'/large'
					);	
					
					$this->image_lib->initialize($config);
					$this->image_lib->resize();
					$this->image_lib->clear();
					// Medium
					$config = array(
							'image_library'=>'gd2',
							'source_image'=>$data['full_path'],
							'thumb_maker' => '_170x198',
							'create_thumb'=>FALSE,
							'maintain_ratio'=>FALSE,
							'quality'=>'100%',
							'width'=>'170',
							'height'=>'198',
							'new_image'=>$data['file_path'].'/medium'
					);	
					
					$this->image_lib->initialize($config);
					$this->image_lib->resize();
					$this->image_lib->clear();
					
					$config = array(
							'image_library'=>'gd2',
							'source_image'=>$data['full_path'],
							'thumb_maker' => '_170x198',
							'create_thumb'=>FALSE,
							'maintain_ratio'=>FALSE,
							'quality'=>'100%',
							'width'=>'86',
							'height'=>'100',
							'new_image'=>$data['file_path'].'/small'
					);	
					
					$this->image_lib->initialize($config); 
					$this->image_lib->resize();
					$this->image_lib->clear();
					
				$data['image_thumb'] = $data['file_name'];	
				$this->product->create_post($lang_working['id'],$data);
				/*
				$post_id = $this->db->insert_id();
				$categories = $this->input->post('categories');
				//$tags = $this->input->post('tags');
		
				foreach ($categories as $category_id)
				{
					$this->product->add_newscategory($post_id, $category_id);
				}
				*/
				//if ($tags)
				//{
				//	$this->posts->add_tags($this->posts->parse_tags($tags), $post_id);
				//}
				$this->session->set_flashdata('message', lang('successfully_created'));
	
				redirect('admin/product', 'refresh');
			}
			
			
			
		}else{
			
		$this->_template['page']	= 'product/create';
			
		$this->system_library->load($this->_template['page'], $data, TRUE);
		}
	}

	public function edit($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
		$this->load->helper('fckeditor');	
		$this->form_validation->set_rules('title', 'lang:form_title', 'required|max_length[200]');
		//$this->form_validation->set_rules('excerpt', 'lang:form_excerpt', 'required|prep_for_form');
		//$this->form_validation->set_rules('content', 'lang:form_content', 'prep_for_form');
		$this->form_validation->set_rules('categories', 'lang:form_categories', 'required|xss_clean');
		$this->form_validation->set_rules('feature', 'lang:form_feature', 'feature');		
		$this->form_validation->set_error_delimiters('', '<br />');	
		$data['post']						= $this->product->get_post($id);
		$data['categories'] 				= $this->product->get_categories();
		$data['settings']['feature'] = ($data['post']['feature'] == 1) ? 'checked' : '';
		//var_dump ($data['post']);
		// initialize upload photo
		$config['upload_path'] = './uploads/products';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '100';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		
		$this->load->library('upload', $config);
		
		$this->upload->initialize($config);
		
		if ($this->form_validation->run() == TRUE)
		{
			if($_FILES['userfile']['name'] == '')
			{
				$this->product->edit_post($lang_working['id']);
				$this->session->set_flashdata('message', lang('successfully_created'));
	
				redirect('admin/product', 'refresh');
			}
			else
			{
				if ( ! $this->upload->do_upload())
				{
					$this->session->set_flashdata('message', $this->upload->display_errors());
					$this->_template['page']	= 'product/edit';
			
					$this->system_library->load($this->_template['page'], null, TRUE);
				}	
				else
				{
					$data =  $this->upload->data();	
							$this->load->library('image_lib');
					// Large
					$config = array(
							'image_library'=>'gd2',
							'source_image'=>$data['full_path'],
							'thumb_maker' => '_170x198',
							'create_thumb'=>FALSE,
							'maintain_ratio'=>FALSE,
							'quality'=>'100%',
							'width'=>'300',
							'height'=>'350',
							'new_image'=>$data['file_path'].'/large'
					);	
					
					$this->image_lib->initialize($config);
					$this->image_lib->resize();
					$this->image_lib->clear();
					// Medium
					$config = array(
							'image_library'=>'gd2',
							'source_image'=>$data['full_path'],
							'thumb_maker' => '_170x198',
							'create_thumb'=>FALSE,
							'maintain_ratio'=>FALSE,
							'quality'=>'100%',
							'width'=>'170',
							'height'=>'198',
							'new_image'=>$data['file_path'].'/medium'
					);	
					
					$this->image_lib->initialize($config);
					$this->image_lib->resize();
					$this->image_lib->clear();
					
					$config = array(
							'image_library'=>'gd2',
							'source_image'=>$data['full_path'],
							'thumb_maker' => '_170x198',
							'create_thumb'=>FALSE,
							'maintain_ratio'=>FALSE,
							'quality'=>'100%',
							'width'=>'86',
							'height'=>'100',
							'new_image'=>$data['file_path'].'/small'
					);	
					
					$this->image_lib->initialize($config); 
					$this->image_lib->resize();
					$this->image_lib->clear();
					//echo $data['file_path']; exit();
					$data['image_thumb'] = $data['file_name'];	
					$this->product->edit_post($lang_working['id'],$data);
					$oldfile = $this->input->post('oldimage');
					$rawfilepath = $data['file_path'].$oldfile;
					$largefilepath = $data['file_path'].'large/'.$oldfile;
					$mediumfilepath = $data['file_path'].'medium/'.$oldfile;
					$smallfilepath = $data['file_path'].'small/'.$oldfile;
					$this->deleteFile($rawfilepath);
					$this->deleteFile($largefilepath);
					$this->deleteFile($mediumfilepath);
					$this->deleteFile($smallfilepath);
					/*
					$post_id = $this->db->insert_id();
					$categories = $this->input->post('categories');
					//$tags = $this->input->post('tags');
			
					foreach ($categories as $category_id)
					{
						$this->product->add_newscategory($post_id, $category_id);
					}
					*/
					//if ($tags)
					//{
					//	$this->posts->add_tags($this->posts->parse_tags($tags), $post_id);
					//}
					$this->session->set_flashdata('message', lang('successfully_created'));
		
					redirect('admin/product', 'refresh');
				
				}
			}
		}
		else
		{
			$this->_template['page']	= 'product/edit';
			
			$this->system_library->load($this->_template['page'], $data, TRUE);
		}
	}

	public function delete($id)
	{
		$this->posts->delete_news($id);
		$this->posts->delete_newscategory($news_id);
		
		$this->session->set_flashdata('message', lang('successfully_deleted'));

		redirect('admin/news', 'refresh');
	}

	public function deleteFile($file) {
		
		if (is_file($file)) 
			unlink($file);
	}
}

/* End of file posts.php */
/* Location: ./application/modules/admin/controllers/posts.php */